package cn.live.kyle.questionnaire.question;

import java.util.ArrayList;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import cn.live.kyle.questionnaire.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class QuestionPage {

	private Element mElement;
	private ViewGroup mContainer;
	private ArrayList<QuestionItem> mQuestionItemList;
	private View mPageView;

	private View mPrePageButton;
	private View mNextPageButton;

	public QuestionPage(Element element, ViewGroup container) {
		mElement = element;
		mContainer = container;

		mQuestionItemList = new ArrayList<QuestionItem>();

		Context context = mContainer.getContext();
		mPageView = LayoutInflater.from(context).inflate(
				R.layout.question_page, mContainer, false);
		mPrePageButton = mPageView.findViewById(R.id.prePage);
		mNextPageButton = mPageView.findViewById(R.id.nextPage);
	}

	public View getPageView() {
		return mPageView;
	}

	public ViewGroup getQuestionViewContainer() {
		return (ViewGroup) mPageView.findViewById(R.id.questionContainer);
	}

	public void preparePageTitle() {
		String pageTitle = mElement.getAttribute(QuestionDomParser.ATTR_TITLE);
		TextView titleView = (TextView) mPageView
				.findViewById(R.id.questionPageTitle);
		if (pageTitle == null || pageTitle.length() <= 0) {
			QuestionItem questionItem = mQuestionItemList.get(0);
			pageTitle = questionItem.getQuestionItemTitle();
			questionItem.hideQuestionItemTiteView(true);
		}

		titleView.setText(pageTitle);
	}

	public void prepareQuestionItem() {
		ViewGroup questionViewContainer = getQuestionViewContainer();
		NodeList questionList = mElement
				.getElementsByTagName(QuestionDomParser.TAG_QUESTION);
		for (int i = 0; i < questionList.getLength(); i++) {
			Element question = (Element) questionList.item(i);
			QuestionItem questionItem = QuestionItemFactory.getQuestionItem(
					question, QuestionPage.this);
			mQuestionItemList.add(questionItem);
			questionViewContainer.addView(questionItem.getQuestionItemView());
		}
	}

	public void onQuestionItemStateChanged() {
		boolean enabled = true;
		for (int i = 0; i < mQuestionItemList.size(); i++) {
			QuestionItem questionItem = mQuestionItemList.get(i);
			enabled = (enabled && questionItem.isAnswered());
		}

		mNextPageButton.setEnabled(enabled);
	}
	
    public void completePage() {
        if (mNextPageButton.isEnabled() && mQuestionItemList.size() == 1
                && mQuestionItemList.get(0) instanceof QuestionRadio) {
            mNextPageButton.performClick();
        }
    }

	public void onFinshPage() {
		for (QuestionItem questionItem : mQuestionItemList) {
			questionItem.onFinish();
		}
	}

	public void setPrePageButtonListener(View.OnClickListener listener) {
		mPrePageButton.setOnClickListener(listener);
	}

	public void setNextPageButtonListener(View.OnClickListener listener) {
		mNextPageButton.setOnClickListener(listener);
	}

	public void setPrePageButtonEnabled(boolean enabled) {
		mPrePageButton.setEnabled(enabled);
	}

	public void setNextPageButtonEnabled(boolean enabled) {
		mNextPageButton.setEnabled(enabled);
	}

	public void setNextButtonTitle(String title) {
		((TextView) mNextPageButton).setText(title);
	}
}
